package edu.scu.lijobs.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import edu.scu.lijobs.android.R.id;
import edu.scu.lijobs.android.R.layout;
import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.schema.Company;
import com.google.code.linkedinapi.schema.Job;

import android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class JobResultRowAdapter extends ArrayAdapter<Job>{

    Context context; 
    List<Job> jobs;
    
    public JobResultRowAdapter(Context context, List<Job> jobs) {
    	super(context, layout.list_view_item_row, jobs);
        this.context = context;
        this.jobs = jobs;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        JobResultHolder  holder = null;
        
        if(row == null)
        {
        
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layout.list_view_item_row, parent, false);
            
            holder = new JobResultHolder();
            holder.imgIcon = (ImageView)row.findViewById(id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(id.txtTitle);
            holder.location = (TextView)row.findViewById(id.location);
            holder.detailJobButton = (Button)row.findViewById(id.detailJobButton);
            row.setTag(holder);
        }
        else
        {
            holder = (JobResultHolder)row.getTag();
        }
        
        final Job job = jobs.get(position);
        holder.txtTitle.setText(job.getPosition().getTitle());
        if(job.getPosition().getLocation() != null) {
        	String date = job.getPostingDate().getMonth() + "/" + job.getPostingDate().getDay() + "/" + job.getPostingDate().getYear();
        	 holder.location.setText(date + " [" + job.getPosition().getLocation().getName() + "]");
        }
        if(job.getCompany() != null) {
        	URL companyURL = getCompanyLogoURL(job.getCompany());
            if(companyURL != null) {
            	try {
            		 Drawable resource = getResource(companyURL); 
            		 Bitmap d = ((BitmapDrawable)resource).getBitmap();
            		 Bitmap bitmapOrig = Bitmap.createScaledBitmap(d, 60,30, true);
                	 holder.imgIcon.setImageBitmap(bitmapOrig);
                	 
                    
            	} catch(Exception e) {
            		e.printStackTrace();
            	}
            	 
            }
        }
        
        
        holder.detailJobButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				try {
					
					Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(job.getSiteJobUrl()));  
					UiHelper.jobResultsFragment.getActivity().startActivity(launchBrowser);
//					Intent myIntent = new Intent( UiHelper.jobResultsFragment.getActivity(), DetailJobActivity.class);
//					UiHelper.jobResultsFragment.getActivity().startActivityForResult(myIntent, 0);
				} catch(Exception e) {
					e.printStackTrace();
				}
				
			}
		});
//        
        return row;
    }
    
    
    private URL getCompanyLogoURL(Company company) {
    	
    	
    		try {
				return new URL(company.getLogoUrl());
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
    	
    	
    	
    }
    
    public Drawable getResource(final URL url)
    {
    
    	try {
    		FutureTask<Drawable> futureTask = new FutureTask<Drawable>(new Callable<Drawable>()
    				{

						@Override
						public Drawable call() throws Exception {
							
							return Drawable.createFromStream((InputStream)url.getContent(), "src");
						}
    			
    		});
    		Thread thread = new Thread(futureTask);
    		thread.start();
    		return futureTask.get();
    			
			
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
    
    }
    
    static class JobResultHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView location;
        Button detailJobButton;
    }
}